// Line3d.prototype = inherit(Entity.prototype);
// Line3d.prototype.constructor = Line3d;
import { Equals, proxy } from "../utils";
import { LineCmp } from "../utils/lineUtil";
import { isPt } from "./Point3d";
import { Vector2d } from "./Vector2d";
import _ from 'lodash';
import { lineScenes } from "../scenes";

export function isLine(line) {
  if (line instanceof Line3d) return true;
  return false;
}
let vid = 0;
export class Line3d {
  constructor() {
    this.data = {
      color: 'red',
      startPt: null,
      endPt: null,
      length: null,
    }
    this.ctx = null;
    this.vid = vid;
    ++vid;
    this.initState();
  }
  initState() {
    const instance = this;
    Object.keys(this.data).forEach(key => {
      proxy(instance, `_proxy`, key)
    });
  }
  setStartPt(pt) {
    this.startPt = pt;
  }
  setEndPt(pt) {
    this.endPt = pt;
    this.length = LineCmp.distPt(this.startPt, pt).toFixed(2);
  }
  setColor(color) {
    this.color = color;
  }
  setCtx(ctx) {
    this.ctx = ctx;
  }
  render(isUp) {
    this.ctx.lineWidth = 1;
    this.ctx.strokeStyle = this.color;
    this.ctx.beginPath();
    this.ctx.moveTo(this.startPt.x, this.startPt.y);
    this.ctx.lineTo(this.endPt.x, this.endPt.y);
    this.ctx.stroke();
    if (isUp) {
      const newEntity = _.cloneDeep(this);
      lineScenes.push(newEntity);
    }
    // if(guidewires){
    //   this.drawGuidewires();
    // }
  }
  get vec() {
    return new Vector2d(this.startPt, this.endPt);
  }
  isLeft(pt) {
    if (!isPt(pt)) return;
    const vec = new Vector2d(this.startPt, this.endPt);
    const n = vec.n;
    const d = n.dotProduct(this.startPt);
    return n.dotProduct(pt) - d > 0
  }
  isRight(pt) {
    if (!isPt(pt)) return;
    const vec = new Vector2d(this.startPt, this.endPt);
    const n = vec.n;
    const d = n.dotProduct(this.startPt);
    return n.dotProduct(pt) - d < 0
  }
  drawGuidewires() {
    this.ctx.save();
    this.ctx.strokeStyle = 'rgba(0,0,230,0.4)';
    this.ctx.lineWidth = 0.5;
    this.drawHorizontalLine();
    this.drawVerticalLine();

    this.ctx.restore();
  }
  drawHorizontalLine() {
    if (Equals(this.endPt.y, this.startPt.y, 3)) {
      this.ctx.beginPath();
      this.ctx.moveTo(this.startPt.x, this.startPt.y + 0.5);
      this.ctx.lineTo(this.ctx.canvas.width, this.endPt.y + 0.5);
      this.ctx.stroke();
    }

  }
  drawVerticalLine() {
    if (Equals(this.endPt.x, this.startPt.x, 3)) {
      this.ctx.beginPath();
      this.ctx.moveTo(this.startPt.x, this.startPt.y + 0.5);
      this.ctx.lineTo(this.endPt.x, 0);
      this.ctx.stroke();
    }
    // ctx.beginPath();
    // ctx.moveTo(x+0.5,0);
    // ctx.lineTo(x+0.5,ctx.canvas.height);
    // ctx.stroke();
  }

}

// Object.assign(Line3d.prototype, {
//   toString() { },
//   toValue() { }
// });